/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layouteditor.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import layoutitems.CurrentLayoutElements;
import layoutitems.ImageElement;

/**
 *
 * @author ST
 */
public class ElementRightClickMenu implements ActionListener{
    
    //#######################
    //      Fields
    //#######################
    
    private static JPopupMenu popUpMenu;
    private static JMenuItem menuDeleteItem;
    private static JMenuItem menuOneLayerUp;
    private static JMenuItem menuOneLayerDown;
    private static JComponent frame;
    private static ImageElement markedElement;
    
    //Menu initialisieren
    public ElementRightClickMenu (JComponent jFrame) {
        frame = jFrame;
        popUpMenu = new JPopupMenu();
        menuDeleteItem = new JMenuItem();
        menuOneLayerUp = new JMenuItem();
        menuOneLayerDown = new JMenuItem();
        //Löschen
        menuDeleteItem.setText("Element löschen");
        menuDeleteItem.addActionListener(this);
        popUpMenu.add(menuDeleteItem);
        //einen Layer rauf
        menuOneLayerUp.setText("Element eine Ebene höher");
        menuOneLayerUp.addActionListener(this);
        popUpMenu.add(menuOneLayerUp);
        //einen Layer rauf
        menuOneLayerDown.setText("Element eine Ebene niedriger");
        menuOneLayerDown.addActionListener(this);
        popUpMenu.add(menuOneLayerDown);
    }
    //##########################
    //      Inner Methods
    //##########################
    
    public void showMenu(JComponent frameLayeredPane,ImageElement marked,int x,int y){
        markedElement = marked;
        popUpMenu.show(frameLayeredPane, x, y);
    }
    
    //##########################
    //      ActionListener
    //##########################
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == menuDeleteItem) {
            if (markedElement != null) {
                if (CurrentLayoutElements.deleteLayoutItem(markedElement)) {
                    frame.repaint();
                }
            }
        }
        if (e.getSource() == menuOneLayerUp) {
            if (markedElement != null) {
                if (CurrentLayoutElements.moveItemUp(markedElement)) {
                    frame.repaint();
                }
            }
        }
        if (e.getSource() == menuOneLayerDown) {
            if (markedElement != null) {
                if (CurrentLayoutElements.moveItemDown(markedElement)) {
                    frame.repaint();
                }
            }
        }
    };
}
